<template>
  <div class="header">
    <a href="#/">
      <img class="logo" src="@/assets/logo.png" width="25px" />
      <span class="company">会员管理系统</span>
    </a>

    <!-- 修改密码/退出系统 -->
    <el-dropdown @command="handleCommand">
      <span class="el-dropdown-link">
        {{user.name}}
        <i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item icon="el-icon-edit" command="a">修改密码</el-dropdown-item>
        <el-dropdown-item icon="el-icon-s-fold" command="b">退出系统</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

    <!-- 弹出修改密码窗口 -->
    <el-dialog title="修改密码" :visible.sync="dialogFormVisible" width="400px">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
        style="width:300px"
      >
        <el-form-item label="原密码" prop="oldPass">
          <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPass">
          <el-input type="password" v-model="ruleForm.newPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="$refs['ruleForm'].resetFields()">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { logout } from "@/api/login";
import passwordApi from "@/api/password";

export default {
  data() {
    const validateoldPass = (rule, value, callback) => {
      passwordApi.checkPwd(this.user.id, value).then(response => {
        const resp = response.data;
        // console.log(resp);
        if (resp.flag) {
          callback();
        } else {
          callback(new Error(resp.message));
        }
      });
    };

    const validatecheckPass = (rule, value, callback) => {
      if (value !== this.ruleForm.newPass) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };

    return {
      // user: JSON.parse(localStorage.getItem("mxg-msm-user")),
      user: this.$store.state.user.user,
      dialogFormVisible: false,
      ruleForm: {
        oldPass: "",
        newPass: "",
        checkPass: ""
      },
      rules: {
        oldPass: [
          { required: true, message: "原密码不能为空", trigger: "blur" },
          {
            validator: validateoldPass,
            trigger: "blur"
          }
        ],
        newPass: [
          { required: true, message: "新密码不能为空", trigger: "blur" }
        ],
        checkPass: [
          { validator: validatecheckPass, trigger: "change" },
          { required: true, message: "确认密码不能为空", trigger: "blur" }
        ]
      }
    };
  },

  methods: {
    handleCommand(command) {
      switch (command) {
        //修改密码
        case "a":
          this.handlePwd();
          break;
        //退出系统
        case "b":
          this.handleLogout();
          break;

        default:
          break;
      }
    },

    // 退出系统
    handleLogout() {
      this.$store.dispatch("Logout").then(response => {
        //退出成功
        if (response.flag) {
          // 回到登录界面
          this.$router.push("/login");
        } else {
          this.$message({
            message: resp.message,
            type: "warning"
          });
        }
      });
    },

    // 修改密码
    handlePwd() {
      this.dialogFormVisible = true;
      this.$nextTick(() => {
        this.$refs["ruleForm"].resetFields();
      });
    },

    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          passwordApi
            .updatePwd(this.user.id, this.ruleForm.checkPass)
            .then(response => {
              const resp = response.data;
              this.$message({
                message: resp.message,
                type: resp.flag ? "success" : "warning"
              });
              if (resp.flag) {
                this.dialogFormVisible = false;
                this.handleLogout();
              }
            });
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.logo {
  vertical-align: middle;
  padding: 0px 10px 0px 40px;
}
.company {
  position: absolute;
  color: white;
}

/* 下拉菜单 */
.el-dropdown {
  float: right;
  margin-right: 40px;
}

.el-dropdown-link {
  color: #fff;
  cursor: pointer;
}
</style>
